package com.hoshiicloud.market.service.impl.commission;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.commission.CommissionRecordsType;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.commission.CommissionRecordsTypeMapper;
import com.hoshiicloud.market.service.commission.CommissionRecordsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:campchen
 * @Date:Created in 上午 10:04 2019/7/29
 * @PACKAGENAME com.hoshiicloud.market.service.impl.commission
 * @Description
 */
@Service
public class CommissionRecordsTypeServiceImpl extends ServiceImpl<CommissionRecordsTypeMapper, CommissionRecordsType> implements CommissionRecordsTypeService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Cacheable(cacheNames = "CommissionRecordsType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public CommissionRecordsType getCommissionRecordsTypeById(Long id) {
        return baseMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionRecordsType", allEntries = true)
    public boolean insert(CommissionRecordsType commissionRecordsType) {
        String id=idGeneratorService.queryGeneratorId();
        if (id==null||"".equals(id)){
            throw new ServiceException(CommonError.error("id生成有误！"));
        }
        commissionRecordsType.setId(Long.parseLong(id));
        commissionRecordsType.setDeleted(0);
        commissionRecordsType.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(commissionRecordsType);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionRecordsType", allEntries = true)
    public boolean updateById(CommissionRecordsType commissionRecordsType) {
        int row = baseMapper.updateById(commissionRecordsType);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Cacheable(cacheNames = "CommissionRecordsType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public IPage<CommissionRecordsType> getPage(IPage<CommissionRecordsType> page, Wrapper<CommissionRecordsType> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
